#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
/*
 * Zenlayer.com Inc.
 * Copyright (c) 2014-2021 All Rights Reserved.
 */
package ${package}.${artifactId}.integration.impl;

import ${package}.${artifactId}.config.amqp.callback.RabbitConfirmCallback;
import ${package}.${artifactId}.config.amqp.callback.RabbitReturnCallback;
import ${package}.${artifactId}.integration.MQService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.${artifactId}.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Objects;
import java.util.UUID;

/**
 * @author wangjunjie
 * @version ${symbol_dollar} Id: RabbitMqServiceImpl.java, v 0.1  wangjunjie Exp ${symbol_dollar}
 * @date 2019-09-18 14:58:02
 */
@Service
public class RabbitMqServiceImpl implements MQService {
    private static final Logger logger = LoggerFactory.getLogger(RabbitMqServiceImpl.class);

    @Resource
    @Lazy
    private RabbitTemplate rabbitTemplate;

    @Value("${symbol_dollar}{spring.rabbitmq.enable}")
    private boolean enable;

    @PostConstruct
    public void initRabbitTemplate() {
        if (!enable) {
            return;
        }
        // 设置生产者消息确认
        rabbitTemplate.setConfirmCallback(new RabbitConfirmCallback());
        rabbitTemplate.setReturnsCallback(new RabbitReturnCallback());
    }


    @Override
    public void sendMessage(String exchange, String routingKey, Object message) {
        if (Objects.nonNull(message)) {
            rabbitTemplate.convertAndSend(exchange, routingKey, message, new CorrelationData("Routing-" + UUID.randomUUID()));
            logger.info("发送一条消息,exchange:[{}],routingKey:[{}],message:[{}]", exchange, routingKey, message);
        }
    }
}
